/**
 * 
 */
package mapmaker;

import java.util.Timer;

import mapmaker.config.Config;
import mapmaker.map.EnvironmentMap;
import mapmaker.pose.Position;
import mapmaker.robot.Robot;
import mapmaker.thread.ThreadCheckLaser;
import mapmaker.thread.ThreadExplore;
import mapmaker.thread.ThreadSaveMap;

public class MapMakerMain {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		if(args.length < 5){
			System.out.println("Usage : MapMakerMain <server_url> <x1> <y1> <x2> <y2>");
		}else{
			// Server configuration and map creation
			Config.SERVER_URL = args[0];
			Position botLeft = new Position(Double.valueOf(args[1]), Double.valueOf(args[2]), 0);
			Position topRight = new Position(Double.valueOf(args[3]), Double.valueOf(args[4]), 0);
			EnvironmentMap map = new EnvironmentMap(botLeft, topRight);
			
			Robot r = new Robot();
			// Thread creation
			Timer timerMapSaver = new Timer();
			timerMapSaver.schedule(new ThreadSaveMap(map, Config.MAP_FILE_NAME), 0, Config.PERIOD_MAPSAVER);
			Thread mapExplorer = new Thread(new ThreadExplore(r));
			mapExplorer.start();
			Thread checkLaser = new Thread(new ThreadCheckLaser(r));
			checkLaser.start();
			r.setEnvironmentMap(map);
			// Start creation of the map
			r.createMap();
			// End
			try {
				mapExplorer.join(0);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			try {
				checkLaser.join(0);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			map.generateMapImage(Config.MAP_FILE_NAME);
		}
	}

}
